package grape.attend.rest.user.mvc;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.attend.service.user.api.IUserClockDateService;
import grape.attend.service.user.po.UserClockDate;
import grape.attend.service.user.query.UserClockDateListQuery;
import grape.common.exception.ExceptionTools;
import io.swagger.annotations.ApiParam;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.attend.rest.user.form.UserOvertimeCreateForm;
import grape.attend.rest.user.form.UserOvertimeUpdateForm;
import grape.attend.rest.user.form.UserOvertimeListPageForm;
import grape.attend.rest.user.vo.UserOvertimeVo;
import grape.attend.rest.user.mapper.UserOvertimeWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.attend.service.user.po.UserOvertime;
import grape.attend.service.user.api.IUserOvertimeService;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
/**
 * <p>
 * 用户加班表 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-05-18
 */
@RestController
@RequestMapping("/attend/userovertime")
@Api(tags = "用户加班表相关接口")
public class UserOvertimeController extends BaseController<UserOvertimeVo, UserOvertime> {

    @Autowired
    private UserOvertimeWebMapper currentWebMapper;
    @Autowired
    private IUserOvertimeService currentService;

    @Autowired
    private IUserClockDateService iUserClockDateService;


     @ApiOperation("添加用户加班表")
     @PreAuthorize("hasAuthority('userovertime:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public UserOvertimeVo create(@RequestBody @Valid UserOvertimeCreateForm cf) {
         UserOvertime po = currentWebMapper.formToPo(cf);
         return super.create(po);
     }

     @ApiOperation("根据ID查询用户加班表")
     @PreAuthorize("hasAuthority('userovertime:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public UserOvertimeVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除用户加班表")
     @PreAuthorize("hasAuthority('userovertime:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新用户加班表")
     @PreAuthorize("hasAuthority('userovertime:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public UserOvertimeVo update(@PathVariable String id,@RequestBody @Valid UserOvertimeUpdateForm uf) {
         UserOvertime po = currentWebMapper.formToPo(uf);
         po.setId(id);
         return super.update(po);
     }

    @ApiOperation("分页查询用户加班表")
    @PreAuthorize("hasAuthority('userovertime:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<UserOvertimeVo> listPage(UserOvertimeListPageForm listPageForm) {
         UserOvertime po = currentWebMapper.formToPo(listPageForm);
         return super.listPage(po,listPageForm);
     }

    /**
     * 按月份查询当前登录用户的加班
     * @param monthMs 所在月份的时间戳
     * @param loginUserId
     * @return
     */
    @ApiOperation("查询当前登录用户加班")
    @PreAuthorize("hasAuthority('userovertime:single:myList')")
    @GetMapping("/my/list")
    @ResponseStatus(HttpStatus.OK)
    public List<UserOvertimeVo> myList(@ApiParam(required = true) Long monthMs, @ApiIgnore String loginUserId) {
        if(monthMs == null){
            monthMs = DateUtil.current(false);
        }
        List<UserOvertime> userOvertimes = currentService.getByUserIdAndMonth(monthMs, loginUserId);
        // 如果没有班次日期，则肯定没有加班记录
        if (isEmpty(userOvertimes)) {
            throw ExceptionTools.dataNotExistRE("暂无加班记录");
        }

        return super.posToVos(userOvertimes);
    }
}
